package org.jspresso.framework.view.descriptor.cardView;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.Subject;

import org.jspresso.framework.model.descriptor.basic.BasicBinaryPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicBooleanPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicDatePropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicDecimalPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicEnumerationPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicIntegerPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicPercentPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicStringPropertyDescriptor;
import org.jspresso.framework.model.descriptor.basic.BasicTimePropertyDescriptor;
import org.jspresso.framework.view.descriptor.IPropertyViewDescriptor;
import org.jspresso.framework.view.descriptor.IViewDescriptor;
import org.jspresso.framework.view.descriptor.basic.BasicBorderViewDescriptor;
import org.jspresso.framework.view.descriptor.basic.BasicCardViewDescriptor;
import org.jspresso.framework.view.descriptor.basic.BasicComponentViewDescriptor;
import org.jspresso.framework.view.descriptor.basic.BasicPropertyViewDescriptor;
import org.jspresso.model.ActionTestInput;
import org.jspresso.model.ActionTestIo;

public class ActionTestIoCarViewDescriptor extends BasicCardViewDescriptor {

	IViewDescriptor defaultView = null;

	@Override
	public String getCardNameForModel(Object model, Subject subject) {
		if(model != null)
		{
			ActionTestIo input = ((ActionTestIo) model);
			if(input.isSql()) {
				return "SQL" + (input instanceof ActionTestInput ? ",INPUT" : ",OUTPUT") + (input.isPrevOutputAsSource() ? ",PREV" : ",LOCAL");
			}
			else if (input.getParameterType() != null ) {
				return input.getParameterType() + (input instanceof ActionTestInput ? ",INPUT" : ",OUTPUT") + (input.isPrevOutputAsSource() ? ",PREV" : ",LOCAL");
			}
		}
		return null;
	}

	@Override
	public IViewDescriptor getCardViewDescriptor(String cardName) {
		if(super.getCardViewDescriptor(cardName) == null) {
			if(cardName != null) {

				String parmeterType = cardName.split(",")[0];
				String ioType = cardName.split(",")[1];
				String source = cardName.split(",")[2];

				BasicPropertyDescriptor modelDescriptor = null;
				boolean isRef = false;
				boolean isList = false;
				boolean isPrevOutput = source.equals("PREV");

				switch(parmeterType) {
				case ActionTestIo.PARAMETER_TYPE_BINARY: 
					modelDescriptor = new BasicBinaryPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_BOOL: 
					modelDescriptor = new BasicBooleanPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_DATE: 
					modelDescriptor = new BasicDatePropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_DATE_TIME: 
					modelDescriptor = new BasicDatePropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_DECIMAL: 
					modelDescriptor = new BasicDecimalPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_ENUMERATION: 
					modelDescriptor = new BasicEnumerationPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_INTEGER: 
					modelDescriptor = new BasicIntegerPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_LIST: 
					isList = true;
					break;
				case ActionTestIo.PARAMETER_TYPE_PERCENT: 
					modelDescriptor = new BasicPercentPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_REF: 
					isRef = true;
					break;
				case ActionTestIo.PARAMETER_TYPE_STRING: 
				case "SQL":
					modelDescriptor = new BasicStringPropertyDescriptor();
					break;
				case ActionTestIo.PARAMETER_TYPE_TIME:			
					modelDescriptor = new BasicTimePropertyDescriptor();
					break;			
				default: break;
				}

				BasicBorderViewDescriptor viewDescClone = (BasicBorderViewDescriptor) ((BasicBorderViewDescriptor)getDefaultView()).clone();
				if(!isList || isPrevOutput) {
					viewDescClone.setSouthViewDescriptor(null);
				}
				BasicComponentViewDescriptor centerViewDesc = (BasicComponentViewDescriptor) ((BasicComponentViewDescriptor) (((BasicBorderViewDescriptor)viewDescClone).getCenterViewDescriptor())).clone();
				List<IPropertyViewDescriptor> acceptedDescriptors = new ArrayList<IPropertyViewDescriptor>();
				for(IPropertyViewDescriptor propertyDesc : centerViewDesc.getPropertyViewDescriptors()) {
					if(isPrevOutput) {
						if(!propertyDesc.getName().equals(ActionTestIo.PARAMETER_SQL_VALUE)
								&& !(propertyDesc.getName().equals(ActionTestIo.PARAMETER_REF_VALUE))
								&& !(propertyDesc.getName().equals(ActionTestIo.PARAMETER_SQL_VALUE))
								&& !(propertyDesc.getName().equals(ActionTestIo.PARAMETER_REF_VALUES))) {
							acceptedDescriptors.add(propertyDesc);
						}
					}
					else if(isRef) {
						if(!propertyDesc.getName().equals(ActionTestIo.PARAMETER_SQL_VALUE) && !(propertyDesc.getName().equals(ActionTestIo.PREV_OUTPUT_PARAMETER_KEY))
								/*&& !(propertyDesc.getName().equals(ActionTestIo.PARAMETER_REF_VALUE) && ioType.equals("INPUT") && propertyDesc.getActionMap() != null)
								&& !(propertyDesc.getName().equals(ActionTestIo.PARAMETER_REF_VALUE) && ioType.equals("OUTPUT") && propertyDesc.getActionMap() == null)*/) {
							acceptedDescriptors.add(propertyDesc);
						}
					}
					else {
						if(!propertyDesc.getName().equals(ActionTestIo.PARAMETER_REF_VALUE) && !propertyDesc.getName().equals(ActionTestIo.PARAMETER_SQL_VALUE)) {
							acceptedDescriptors.add(propertyDesc);
						}
					}
				}

				if(!isList && !isRef && !isPrevOutput) {
					modelDescriptor.setName(ActionTestIo.PARAMETER_SCALAR_VALUE);
					BasicPropertyViewDescriptor stringDescriptor = new BasicPropertyViewDescriptor();
					stringDescriptor.setName(ActionTestIo.PARAMETER_SCALAR_VALUE);
					stringDescriptor.setModelDescriptor(modelDescriptor);
					acceptedDescriptors.add(stringDescriptor);
				}

				centerViewDesc.setPropertyViewDescriptors(acceptedDescriptors);
				viewDescClone.setCenterViewDescriptor(centerViewDesc);

				super.putCardViewDescriptor(cardName, viewDescClone);
			}
		}

		return super.getCardViewDescriptor(cardName);
	}

	public IViewDescriptor getDefaultView() {
		return defaultView;
	}

	public void setDefaultView(IViewDescriptor defaultView) {
		this.defaultView = defaultView;
	}
}
